﻿namespace Hidistro.UI.SaleSystem.Tags
{
    using Hidistro.Core;
    using Hidistro.Entities.Commodities;
    using Hidistro.SaleSystem.Catalog;
    using JSShop.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class YB_Brand_AttributeList : WebControl
    {
        private int brandId;
        private int categoryId;
        private string valueStr = string.Empty;

        private string CreateUrl(string paraName, string paraValue)
        {
            string rawUrl = this.Context.Request.RawUrl;
            if (rawUrl.IndexOf("?") >= 0)
            {
                string oldValue = rawUrl.Substring(rawUrl.IndexOf("?") + 1);
                string[] strArray = oldValue.Split(new char[] { Convert.ToChar("&") });
                rawUrl = rawUrl.Replace(oldValue, "");
                foreach (string str3 in strArray)
                {
                    if (!str3.ToLower().StartsWith(paraName + "=") && !str3.ToLower().StartsWith("pn="))
                    {
                        rawUrl = rawUrl + str3 + "&";
                    }
                }
                return (rawUrl + paraName + "=" + Globals.UrlEncode(paraValue));
            }
            string str4 = rawUrl;
            return (str4 + "?" + paraName + "=" + Globals.UrlEncode(paraValue));
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        private string Chtml()
        {
            if (string.IsNullOrEmpty(this.Context.Request.QueryString["Staticize"]))
            {
                StringBuilder sb = new StringBuilder();
                string bclass = string.Empty;
                List<JSShop.Model.Hishop_BrandCategories> brandCategories = new Juse.Interface.CategoryBrowser().GetBrandList(this.categoryId, 16);
                if ((brandCategories != null) && (brandCategories.Count > 0))
                {

                    foreach (Hishop_BrandCategories row in brandCategories)
                    {
                        if (this.brandId == row.BrandId)
                        {
                            bclass = "class=\"kkokk\"";
                            string shouye = "<a href=" + Globals.GetSiteUrls().SubBrandDetails(row.BrandId, row.RewriteName) + ">主页</a>";
                            sb.Insert(0, shouye);
                            sb.AppendFormat("<a href=\"{0}\"{2}>{1}</a>", Globals.GetSiteUrls().SubBrandDetails(row.BrandId, row.RewriteName), row.BrandName, bclass);
                        }
                        else
                        {
                            bclass = "";
                            sb.AppendFormat("<a href=\"{0}\"{2}>{1}</a>", Globals.GetSiteUrls().SubBrandDetails(row.BrandId, row.RewriteName), row.BrandName, bclass);
                        }
                    }
                }
                return sb.ToString();
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                string bclass = string.Empty;
                //string model = "Brand_detail-{0}_cid-0_filter-0_rank-0.html";
                string model = "B{0}C0F0R0.html";
                int filter;
                int rank;

                string url;

                int.TryParse(this.Page.Request.QueryString["filter"], out filter);
                int.TryParse(this.Page.Request.QueryString["rank"], out rank);


                List<JSShop.Model.Hishop_BrandCategories> brandCategories = new Juse.Interface.CategoryBrowser().GetBrandList(this.categoryId, 16);
                if ((brandCategories != null) && (brandCategories.Count > 0))
                {

                    foreach (Hishop_BrandCategories row in brandCategories)
                    {
                        if (this.brandId == row.BrandId)
                        {
                            bclass = "class=\"kkokk\"";
                            //string shouye = "<a href=" + Globals.GetSiteUrls().SubBrandDetails(row.BrandId, row.RewriteName) + ">主页</a>";

                            url = string.Format(model, brandId);
                            string shouye = "<a href=" + url + ">主页</a>";
                            sb.Insert(0, shouye);
                            sb.AppendFormat("<a href=\"{0}\"{2}>{1}</a>", url, row.BrandName, bclass);
                        }
                        else
                        {
                            url = string.Format(model, row.BrandId);
                            bclass = "";
                            sb.AppendFormat("<a href=\"{0}\"{2}>{1}</a>", url, row.BrandName, bclass);
                        }
                    }
                }
                return sb.ToString();
            }

        }
        private string Bhtml()
        {
            if (string.IsNullOrEmpty(this.Context.Request.QueryString["Staticize"]))
            {
                StringBuilder sb = new StringBuilder();
                Hishop_BrandCategories brandCategories = new Juse.Interface.ProductBrowser().GetBrandInfo(this.brandId);
                if (brandCategories != null)
                {

                    sb.AppendFormat("<a href=\"{0}\"{1}>主页</a>", Globals.GetSiteUrls().SubBrandDetails(brandCategories.BrandId, brandCategories.RewriteName), "");
                    sb.AppendFormat("<a href=\"{0}\"{1}>{2}</a>", Globals.GetSiteUrls().SubBrandDetails(brandCategories.BrandId, brandCategories.RewriteName), "class=\"kkokk\"", brandCategories.BrandName);

                }
                return sb.ToString();
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                Hishop_BrandCategories brandCategories = new Juse.Interface.ProductBrowser().GetBrandInfo(this.brandId);
                //string model = "Brand_detail-{0}_cid-0_filter-0_rank-0.html";
                string model = "B{0}C0F0R0.html";
                int filter;
                int rank;

                string url;

                int.TryParse(this.Page.Request.QueryString["filter"], out filter);
                int.TryParse(this.Page.Request.QueryString["rank"], out rank);
                url = string.Format(model, brandCategories.BrandId);
                if (brandCategories != null)
                {

                    sb.AppendFormat("<a href=\"{0}\"{1}>主页</a>", url, "");
                    sb.AppendFormat("<a href=\"{0}\"{1}>{2}</a>", url, "class=\"kkokk\"", brandCategories.BrandName);

                }
                return sb.ToString();
            }
        }

        string StaticizeBrandDetails(int BrandId)
        {
            string url = this.Page.Request.Url.AbsoluteUri;
            int iend = 0;
            int ibeg = 0;

            if (url.Contains("Brand_detail-"))
            {
                ibeg = url.IndexOf('-');
                iend = url.IndexOf('_', 12);
                string num = url.Substring(ibeg + 1, iend - 1);
                url.Replace(num, BrandId.ToString());
            }



            return url;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            int.TryParse(this.Context.Request.QueryString["cId"], out this.categoryId);
            int.TryParse(this.Context.Request.QueryString["brandId"], out this.brandId);
            string re = string.Empty;
            if (brandId != 0)
            {
                re = Bhtml();
            }
            if (this.categoryId != 0)
            {
                re = Chtml();
            }
            writer.Write(re);
        }
    }
}

